home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / m4-1_0_3.lha / m4-1.0.3 / path.c < prev    next >
C/C++ Source or Header  |  1992-12-19  |  3KB  |  157 lines

  1. /*
  2.  * GNU m4 -- A simple macro processor
  3.  * Copyright (C) 1989-1992 Free Software Foundation, Inc.
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2, or (at your option)
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /*
  21.  * Handling of path search of included files via the builtins "include"
  22.  * and "sinclude".
  23.  */
  24.  
  25. #include "m4.h"
  26.  
  27.  
  28. struct includes
  29. {
  30.   struct includes *next;    /* next directory to search */
  31.   char *dir;            /* directory */
  32.   int len;
  33. };
  34.  
  35. typedef struct includes includes;
  36.  
  37. static includes *dir_list;        /* the list of path directories */
  38. static includes *dir_list_end;        /* the end of same */
  39. static int dir_max_length;        /* length of longest directory name */
  40.  
  41.  
  42. void
  43. include_init (void)
  44. {
  45.   dir_list = NULL;
  46.   dir_list_end = NULL;
  47.   dir_max_length = 0;
  48. }
  49.  
  50. void
  51. include_env_init (void)
  52. {
  53.   char *path;
  54.   char *path_end;
  55.  
  56.   if (no_gnu_extensions)
  57.     return;
  58.  
  59.   path = getenv ("M4PATH");
  60.   if (path == NULL)
  61.     return;
  62.  
  63.   do
  64.     {
  65.       path_end = index (path, ':');
  66.       if (path_end != NULL)
  67.     *path_end = '\0';
  68.       add_include_directory (path);
  69.       path = path_end + 1;
  70.     }
  71.   while (path_end != NULL);
  72. }
  73.  
  74. void
  75. add_include_directory (char *dir)
  76. {
  77.   includes *incl;
  78.  
  79.   if (no_gnu_extensions)
  80.     return;
  81.  
  82.   if (*dir == '\0')
  83.     dir = ".";
  84.  
  85.   incl = (includes *) xmalloc (sizeof (struct includes));
  86.   incl->next = NULL;
  87.   incl->len = strlen (dir);
  88.   incl->dir = (char *) xmalloc (incl->len + 1);
  89.   strcpy (incl->dir, dir);
  90.  
  91.   if (incl->len > dir_max_length) /* remember len of longest directory */
  92.     dir_max_length = incl->len;
  93.  
  94.   if (dir_list_end == NULL)
  95.     dir_list = incl;
  96.   else
  97.     dir_list_end->next = incl;
  98.   dir_list_end = incl;
  99.  
  100. #ifdef DEBUG_INCL
  101.   fprintf (stderr, "add_include_directory (%s);\n", dir);
  102. #endif
  103. }
  104.  
  105. FILE *
  106. path_search (const char *dir)
  107. {
  108.   FILE *fp;
  109.   includes *incl;
  110.   char *name;            /* buffer for constructed name */
  111.  
  112.   /* Look in current working directory first.  */
  113.   fp = fopen (dir, "r");
  114.   if (fp != NULL)
  115.     return fp;
  116.  
  117.   /* If file not found, and filename absolute, fail.  */
  118.   if (*dir == '/' || no_gnu_extensions)
  119.     return NULL;
  120.  
  121.   name = (char *) xmalloc (dir_max_length + 1 + strlen (dir) + 1);
  122.  
  123.   for (incl = dir_list; incl != NULL; incl = incl->next)
  124.     {
  125.       strncpy (name, incl->dir, incl->len);
  126.       name[incl->len] = '/';
  127.       strcpy (name + incl->len + 1, dir);
  128.  
  129. #ifdef DEBUG_INCL
  130.       fprintf (stderr, "path_search (%s) -- trying %s\n", dir, name);
  131. #endif
  132.  
  133.       fp = fopen (name, "r");
  134.       if (fp != NULL)
  135.     {
  136.       if (debug_level & DEBUG_TRACE_PATH)
  137.         debug_message ("path search for `%s' found `%s'", dir, name);
  138.       break;
  139.     }
  140.     }
  141.   xfree (name);
  142.   return fp;
  143. }
  144.  
  145.  
  146. #ifdef DEBUG_INCL
  147. static int
  148. include_dump (void)
  149. {
  150.   includes *incl;
  151.  
  152.   fprintf (stderr, "include_dump:\n");
  153.   for (incl = dir_list; incl != NULL; incl = incl->next)
  154.     fprintf (stderr, "\t%s\n", incl->dir);
  155. }
  156. #endif
  157.